#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/stat.h>

int main(int argc,char** argv){
	char filename[]="out.dat";
	int fd=open(filename,O_RDWR);
	if(fd<0){
		printf("open file %s error\n",filename);
		exit(-1);
	}
	//FILE* fp=fopen(filename,"rw");
	struct stat st;
	stat(filename,&st);
	printf("%s filesize = %d fd=%d\n",filename,st.st_size,fd);
	void* mem=mmap(NULL,st.st_size,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);

	printf("mem:%x %c,%c\n",mem,((char*)mem)[0],((char*)mem)[1]);	
	((char*)mem)[0]='x';
	sprintf(mem,"%s size is %d\n",filename,st.st_size);
	msync(mem,st.st_size,MS_SYNC);
	munmap(mem,st.st_size);
	close(fd);
}
